package com.panda.mybatisplus.demo.config;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class MyTableNameHandler implements TableNameHandler {

    private static final ThreadLocal<String> TABLE_NAME_SUFFIX = new ThreadLocal<>();

    public static void setTableNameSuffix(String suffix) {
        TABLE_NAME_SUFFIX.set(suffix);
    }

    @Override
    public String dynamicTableName(String sql, String tableName) {
        String suffix = TABLE_NAME_SUFFIX.get();
        if (StringUtils.isNotBlank(suffix)) {
            TABLE_NAME_SUFFIX.remove();
            String dynamicTableName = tableName + "_" + suffix;
            log.info("dynamicTableName = {}", dynamicTableName);
            return dynamicTableName;
        }
        return tableName;
    }
}
